﻿using RFBR.DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace RFBR.Core.Infrastructure.Models.Structure
{
    internal class Frame : Entity, IFrame
    {
        #region [Private members]
        private IFrame _parent;
        #endregion
        #region [Constructors]
        public Frame(Guid? id, string name = "Frame", EntityType type = EntityType.Frame)
            : base(id.HasValue ? id.Value : Guid.NewGuid(), name, type)
        {
            Slots = new List<ISlot>();
            Childs = new List<IFrame>();
        }
        #endregion
        #region [Properties]
        /// <summary>
        /// Список слотов
        /// </summary>
        public IEnumerable<ISlot> Slots { get; set; }
        /// <summary>
        /// Привязался ли фрейм.
        /// </summary>
        public bool? Attached { get; set; }
        public bool IsSubFrame { get; set; }
        public IFrame Parent
        {
            get
            {
                return _parent;
            }
            set
            {
                _parent = value;
                if (_parent != null && !_parent.Childs.Contains(this))
                {
                    ((List<IFrame>)_parent.Childs).Add(this);
                }
            }
        }
        public IEnumerable<IFrame> Childs { get; set; }
        //public ISlot SlotSuccess
        //{
        //    get
        //    {   
        //        ISlot slot = FindSlotByName(SystemSlot.SUCCESS);
        //        if (slot == null)
        //        {
        //            slot = CreateSlot(SystemSlot.SUCCESS, EntityType.Function);
        //        }
        //        return slot;
        //    }
        //}
        #endregion
        #region [Public methods]
        public ISlot CreateSlot(string name = "Slot", EntityType type = EntityType.Custom, InheritanceType inheritance = InheritanceType.Override)
        {
            ISlot slot = new Slot(name, inheritance);
            ((List<ISlot>)Slots).Add(slot);
            return slot;
        }
        #endregion
        #region [Private methods]
        /// <summary>
        /// Поиск слота по имени
        /// </summary>
        /// <param name="name">имя слота</param>
        /// <returns>слот</returns>
        private ISlot FindSlotByName(string name)
        {
            return Slots.FirstOrDefault(s => s.Name.ToLower() == name.ToLower());
        }

        #endregion
    }
}
